import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_gradient_line.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/list.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/education_quiz_history_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/history/history_controller.dart';

class EducationQuizHistoryView extends StatelessWidget {
  EducationQuizHistoryView({super.key});

  final controller = Get.put(EducationQuizHistoryController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: Text(
          '历史记录',
          style: TextStyle(fontSize: 14.w),
        ),
        centerTitle: true,
      ),
      body: Container(
        color: AppColors.Pure_gray_bg,
        child: CustomEasyRefreshList<EducationQuizHistoryEntity>(
          itemBuilder: _buildItem,
          dataLoad: (int pageNum, int pageSize, {String? search}) {
            return controller.getHistory(pageNum, pageSize);
          },
          padding: EdgeInsets.only(top: 17.w),
        ),
      ),
    );
  }

  /// 单个卡片
  Widget _buildItem(EducationQuizHistoryEntity item, int idx) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.w),
        color: Colors.white,
      ),
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      padding: EdgeInsets.all(14.w),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  Image.asset(AppImages.icon_dot_fade, height: 8.w, width: 8.w),
                  AppGaps.hGap4,
                  Text(
                    '考试：',
                    style: TextStyle(
                      fontSize: 14.w,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  _buildHintText(item.createDate ?? '未知'),
                ],
              ),
              EducationQuizHistoryController.commonRightCorner(item.score, item.totalScore),
            ],
          ),
          _buildCenterData(item),
          _buildItemBottom(item)
        ],
      ),
    );
  }

  /// 中间数量数据
  Widget _buildCenterData(EducationQuizHistoryEntity item) {
    final children = <Widget>[];
    children.addAll(
      [
        _buildCenterDataItem('满分', item.totalScore?.toString()),
        _buildCenterDataItem('得分', item.score?.toString()),
        _buildCenterDataItem('答对', item.answerCount?.toString()),
        _buildCenterDataItem('答错', item.wrongCount?.toString()),
        _buildCenterDataItem('未答', item.unansweredCount?.toString()),
      ]
    );
    children.joinWith(CustomVerticalGradientLine(
      width: 1,
      height: 24.w,
    ));
    return Container(
      decoration: BoxDecoration(
        color: const Color(0xFFEEF0FF),
        borderRadius: BorderRadius.circular(8.w),
      ),
      margin: EdgeInsets.symmetric(vertical: 14.w),
      padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 7.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: children,
      ),
    );
  }

  Widget _buildCenterDataItem(String key, String? val) {
    return Container(
          width: 47.w,
          alignment: Alignment.center,
          child: Column(
            children: [
              Text(
                val ?? '未知',
                style: TextStyle(
                  //fontFamily: 'YouSheBiaoTiHei-Bold',
                  fontSize: 16.w,
                  color: EducationQuizHistoryController.commonColorMapper(key),
                ),
              ),
              Text(
                key,
                style: TextStyle(fontSize: 11.w, color: const Color(0xFF495061)),
              )
            ],
          ),
        );
  }

  Widget _buildItemBottom(EducationQuizHistoryEntity item) {
    final duration =item.questionList?.first.duration ?? 0;
    final seconds = 60 * duration;
    /// 60 -> 00:01:00
    final hour = (seconds ~/ 3600).toString().padLeft(2, '0');
    final minute = (seconds % 3600 ~/ 60).toString().padLeft(2, '0');
    final second = (seconds % 60).toString().padLeft(2, '0');
    final time = '$hour:$minute:$second';
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _buildHintText('限时时长：$time'),
        _buildHintText('答题时间：${item.timeCount}'),
      ],
    );
  }

  Widget _buildHintText(String text) {
    return Text(
      text,
      style: TextStyle(fontSize: 12.w, color: const Color(0xFFA0A7B5)),
    );
  }
}
